package com.vp.org.web.servlet;

import com.vp.org.dao.UserDao;
import com.vp.org.dao.impl.UserDaoImpl;
import com.vp.org.pojo.UserInfo;
import com.vp.org.service.UserService;
import com.vp.org.util.UUIDUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "file", value = "/file.do")
public class FileServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 创建一个新的用户信息对象
        UserInfo user = new UserInfo();

        //创建一个解析器工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        //文件上传解析器
        ServletFileUpload upload = new ServletFileUpload(factory);
        // 判断enctype属性是否为multipart/form-data
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        if (isMultipart) {
            try {
                //解析请求，将表单中每个输入项封装成一个FileItem对象
                List<FileItem> fileItems = upload.parseRequest(request);
                // 迭代表单数据
                String year = "", month = "", birthday;
                for (FileItem fileItem : fileItems) {
                    //判断输入的类型是 普通输入项 还是文件
                    if (fileItem.isFormField()) {
                        //普通输入项 ,得到input中的name属性的值,fileItem.getFieldName()
                        ////得到输入项中的值,fileItem.getString("UTF-8"),"UTF-8"防止中文乱码
                        //System.out.println(fileItem.getFieldName()+"\t"+fileItem.getString("UTF-8"));
                        String value = fileItem.getString("UTF-8");
                        switch (fileItem.getFieldName()) {
                            case "id":
                                user.setUserId(Integer.parseInt(value));
                            case "RadioGroup1":
                                user.setType(value); break;
                            case "realname":
                                user.setUsername(value); break;
                            case "sex":
                                user.setGender(value); break;
                            case "email":
                                user.setEmail(value); break;
                            case "qq":
                                user.setQq(value); break;
                            case "school":
                                user.setSchool(value); break;
                            case "class":
                                user.setClassname(value); break;
                            case "year2":
                                year = value; break;
                            case "month2":
                                month = value; break;
                            case "day2":
                                birthday = year + "-" + month + "-" + value;
                                user.setBirthday(birthday);
                                break;
                            case "hobby":
                                user.setHobby(value); break;
                            case "introduce":
                                user.setIntroduce(value);
                                break;
                        }
                    } else {
                        //上传的是文件，获得文件上传字段中的文件名
                        //注意IE或FireFox中获取的文件名是不一样的，IE中是绝对路径，FireFox中只是文件名
                        String fileName = fileItem.getName();
                        //System.out.println(fileName);
                        //Substring是字符串截取，返回值是一个截取后的字符串
                        //lastIndexOf(".")是从右向左查,获取.之后的字符串
                        String ext = fileName.substring(fileName.lastIndexOf("."));
                        //UUID.randomUUID().toString()是javaJDK提供的一个自动生成主键的方法, UUID的唯一缺陷在于生成的结果串会比较长
                        String name = UUIDUtil.getUUID().substring(0, 11) +ext;

                        user.setPicture("picture/" + name);
                        //将FileItem对象中保存的主体内容保存到某个指定的文件中
                        File file = new File("E:/bookstore/bookmall/out/artifacts/bookmall_war_exploded/upload/picture/" + name);

                        fileItem.write(file);
                    }
                }

                // System.out.println(user);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }else{
            System.out.println("普通表单");
        }

        // 执行修改
        new UserDaoImpl().update(user);

        // 返回前端页面
        response.sendRedirect("/user/center/user_11.jsp?msg=1");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
